/**
 * This file is part of the DbUnit module for NetBeans IDE
 * Copyright (C) 2009 Pavol Pitoňák
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

package cz.muni.fi.dbunit;

import javax.swing.JPanel;
import org.netbeans.api.db.explorer.ConnectionManager;
import org.netbeans.api.db.explorer.DatabaseConnection;
import org.netbeans.api.db.explorer.support.DatabaseExplorerUIs;

/**
 *
 * @author Pavol Pitoňák <pavol@pitonak.com>
 */
public final class DataSetGeneratorVisualPanel extends JPanel {

    public static final String DATABASE_URL = "database_url";
    public static final String DATABASE_USERNAME = "database_username";
    public static final String DATABASE_PASSWD = "database_passwd";
    public static final String DATABASE_SCHEMA = "database_schema";
    
    /** Creates new form DataSetGeneratorVisualPanel */
    public DataSetGeneratorVisualPanel() {
        initComponents();
        DatabaseExplorerUIs.connect(connectionComboBox, ConnectionManager.getDefault());
    }

    @Override
    public String getName() {
        return ""; // doesn't matter, it won't be used
    }

    /**
     *
     * @return chosen connection or null if no connection was chosen
     */
    public String getDatabaseUrl() {
        DatabaseConnection connection = (DatabaseConnection) connectionComboBox.getSelectedItem();
        if (connection == null) {
            return null;
        } else {
            return connection.getDatabaseURL();
        }
    }

    /**
     *
     * @return username associated with chosen connection, null if no connection was chosen
     */
    public String getDatabaseUsername() {
        DatabaseConnection connection = (DatabaseConnection) connectionComboBox.getSelectedItem();
        if (connection == null) {
            return null;
        } else {
            return connection.getUser();
        }
    }

    /**
     *
     * @return password associated with chosen connection, null if no connection was chosen
     */
    public String getDatabasePassword() {
        DatabaseConnection connection = (DatabaseConnection) connectionComboBox.getSelectedItem();
        if (connection == null) {
            return null;
        } else {
            return connection.getPassword();
        }
    }

    /**
     *
     * @return db schema associated with chosen connection, null if no connection was chosen
     */
    public String getDatabaseSchema() {
        DatabaseConnection connection = (DatabaseConnection) connectionComboBox.getSelectedItem();
        if (connection == null) {
            return null;
        } else {
            return connection.getSchema();
        }
    }

    public int getConnectionIndex() {
        return connectionComboBox.getSelectedIndex();
    }

    public void setConnectionIndex(int arg) {
        connectionComboBox.setSelectedIndex(arg);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        connectionLabel = new javax.swing.JLabel();
        connectionComboBox = new javax.swing.JComboBox();

        org.openide.awt.Mnemonics.setLocalizedText(connectionLabel, org.openide.util.NbBundle.getMessage(DataSetGeneratorVisualPanel.class, "DataSetGeneratorVisualPanel.connectionLabel.text")); // NOI18N

        connectionComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(connectionLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(connectionComboBox, 0, 292, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(connectionLabel)
                .addComponent(connectionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox connectionComboBox;
    private javax.swing.JLabel connectionLabel;
    // End of variables declaration//GEN-END:variables
}

